<?php
namespace app\shop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\shop\model\Artwork as ArtworkModel;
use app\shop\model\Artist;
use app\shop\model\Auction;
use app\shop\model\ArtworkGroup;
use think\Hook;

/**
 * 艺术品默认控制器
 * @package app\shop\admin
 */
class Artwork extends Admin
{
    public function index()
    {

        $data_list = ArtworkModel::order('sort,id desc')->paginate();
        $category = ArtworkGroup::where('status',1)->column('id,title');
        return ZBuilder::make('table')
            ->setPageTitle('艺术品管理') 
            ->setSearch(['id' => 'ID', 'title' => '标题'])
            ->addColumns([
                ['id', 'ID'],
                ['title', '艺术品名称'],
                ['group_id', '分类','select',$category],
                ['photos', '缩略图', 'pictures', '无图'],
                ['material', '材质'],
                ['year', '年代'],
                ['create_time', '创建时间', 'datetime'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('add,enable,disable,delete') // 批量添加顶部按钮
            ->addRightButtons('edit,delete') // 批量添加右侧按钮
            ->addFilter('group_id', $category)
            ->setRowList($data_list) // 设置表格数据
            ->fetch(); // 渲染页面
    }

    public function add()
    {

        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Artwork');
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);

            if ($goods = ArtworkModel::create($data)) {
                Hook::listen('artwork_add', $goods);
                // 记录行为
                action_log('artwork_add', 'artwork_add', $goods['id'], UID);
                $this->success('新增成功', url('index'));
            } else {
                $this->error('新增失败');
            }
        }

        $artist = Artist::where('status',1)->column('id,name');
        $auction = Auction::where('status',1)->column('id,title');
        $category = ArtworkGroup::where('status',1)->column('id,title');
         
        return ZBuilder::make('form')
            ->setPageTitle('新增艺术品')
            ->addFormItems([
                ['text', 'title', '名称', '必填，艺术品名称'],
                ['text', 'subtitle', '副标题'],
                ['text', 'material', '材质'],
                ['text', 'size', '尺寸'],
                ['text', 'year', '年代'],
                ['text', 'rongliang', '容量'],
                ['text', 'juanke', '镌刻'],
                ['text', 'gongyi', '工艺'],
                ['date', 'showtime', '开展时间'],
                ['text', 'price', '价格'],
                ['text', 'price_step', '出价幅度'],
                ['text', 'auction_price', '起拍价格'],
                ['radio', 'type', '艺术品类型', '', ['1'=>'一口价', '2'=>'拍卖商品'], 1],
                ['select', 'artist_id', '艺术家', '', $artist],
                ['select', 'group_id', '分类', '', $category],
                ['select', 'auction_id', '所属拍卖活动','',$auction],
                ['date', 'start_time', '拍卖开始时间'],
                ['date', 'endtime', '拍卖结束时间'],
                ['images', 'photos', '多图上传'],
                ['text', 'sort', '排序'],
                ['radio', 'status', '状态', '', ['下架', '上架'], 1],
                ['ckeditor', 'about', '简介']
            ])
            ->fetch();   
    }

    public function edit($id = null)
    {
        if ($id === null) $this->error('缺少参数');

        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();

            if (ArtworkModel::update($data)) {
                $artwork = ArtworkModel::get($data['id']);
                Hook::listen('artist_user_edit', $artwork);
                $this->success('编辑成功');
            } else {
                $this->error('编辑失败');
            }
        }

        // 获取数据
        $info = ArtworkModel::where('id', $id)->find();

        $artist = Artist::where('status',1)->column('id,name');
        $category = ArtworkGroup::where('status',1)->column('id,title');

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('编辑') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['hidden', 'id'],
                ['text', 'title', '名称', '必填，艺术品名称'],
                ['text', 'subtitle', '副标题'],
                ['text', 'material', '材质'],
                ['text', 'size', '尺寸'],
                ['text', 'year', '年代'],
                ['text', 'rongliang', '容量'],
                ['text', 'juanke', '镌刻'],
                ['text', 'gongyi', '工艺'],
                ['date', 'showtime', '开展时间'],
                ['text', 'price', '价格'],
                ['text', 'price_step', '出价幅度'],
                ['text', 'auction_price', '起拍价格'],
                ['radio', 'type', '艺术品类型', '', ['1'=>'一口价', '2'=>'拍卖商品'], 1],
                ['select', 'artist_id', '艺术家', '', $artist],
                ['select', 'group_id', '分类', '', $category],
                ['select', 'auction_id', '所属拍卖活动'],
                ['date', 'start_time', '拍卖开始时间'],
                ['date', 'endtime', '拍卖结束时间'],
                ['images', 'photos', '多图上传'],
                ['text', 'sort', '排序'],
                ['radio', 'status', '状态', '', ['下架', '上架'], 1],
                ['ckeditor', 'about', '简介']
            ])
            ->setFormData($info) // 设置表单数据
            ->fetch();
    }

    public function delete($ids = [])
    {
        Hook::listen('artist_user_delete', $ids);
        return $this->setStatus('delete');
    }


    /**
     * 设置用户状态：删除、禁用、启用
     */
    public function setStatus($type = '', $record = [])
    {
        $ids        = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        $uid_delete = is_array($ids) ? '' : $ids;
        $ids        = array_map('get_nickname', (array)$ids);
        return parent::setStatus($type, ['user_'.$type, 'shop_artwork', $uid_delete, UID, implode('、', $ids)]);
    }
}
